<?php

namespace app\common\model;

use think\Cache;

/**
 * 幸运星词条 模型类
 * Class DivinationTips
 * @package app\common\model
 */
class LuckyTips extends Base
{
    /**
     * 幸运星词条详情（+id）
     */
    const REDIS_KEY_DETAIL = 'lucky_tip_';

    protected $table = 'wechat_backup_07_18_14_43_00.app_lucky_tips';// 定义表名

    /**
     * 获取详情
     * @param array $all_list
     * @return array
     */
    public function getDetails($all_list = [])
    {
        $result = [];
        $ids = $this->getAllNotCachedIds($all_list, self::REDIS_KEY_DETAIL);
        try
        {
            if (strlen($ids) > 0)
            {
                $where = 'id in(' . $ids . ') and status=0';
                $result = $this
                    ->field('id,text')
                    ->where($where)
                    ->order('id DESC')
                    ->select();
                $result = json_decode(json_encode($result, true), true);

                if ($this->cache_on >= 1)
                {
                    foreach ($result as $v)
                    {
                        // 单个词条写入缓存
                        Cache::set(self::REDIS_KEY_DETAIL . $v['id'], $v, $this->cache_time);
                    }
                }
            }
            if ($this->cache_on >= 1)//如果缓存打开，循环获取，因为缓存里已经有了数据
            {
                $result = [];
                foreach ($all_list as $v)
                {
                    $result[] = Cache::get(self::REDIS_KEY_DETAIL . $v['id']);
                }
            }
        }
        catch (\Exception $e)
        {
            // 错误信息写入日志
            \think\Log::write('获取幸运星词条错误:' . '[' . $ids . ']', 'error', true);
        }

        return $result;
    }
}
